package example.documentbased.editorModule.controller
{
	import example.documentbased.editorModule.models.EditorModuleModel;
	import example.documentbased.mainModule.models.vo.EditorVO;
	
	import mx.collections.ArrayList;
	
	import org.etherframework.common.command.ICommand;
	import org.etherframework.common.utils.OU;
	import org.etherframework.controller.IReactiveCommand;
	import org.etherframework.controller.ReactiveCommand;
	
	import raix.reactive.IObservable;
	import raix.reactive.Observable;
	import raix.reactive.Unit;
	
	public class SetEditorCommand extends ReactiveCommand
	{
		private var _editorModel:EditorModuleModel;
		private var _editor:EditorVO;
		
		public function SetEditorCommand(editorModel:EditorModuleModel)
		{
			_editorModel = editorModel;
		}
		
		public function initialize(editor:EditorVO):IReactiveCommand
		{
			_editor = editor;
			markAsInitialized();
			
			return this;
		}
		
		override public function observable():IObservable
		{
			var oldEditor:EditorVO = _editorModel.editor;
			
			_editorModel.editor = _editor;
			
			if (!OU.areEqual(oldEditor, _editor, "id"))
			{
				_editorModel.openedDocuments = new ArrayList();
				_editorModel.currentDocument = null;
				_editorModel.lastSavedDocument = null;
			}
			
			return Observable.value(new Unit());
		}
	}
}